/**
 * 
 */
package com.yi.model.internal.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.yi.model.db.mongo.MongoDBHelper;
import com.yi.model.internal.IModelUIConfigApi;
import com.yi.model.objects.DBDefinitions;
import com.yi.model.objects.Field;
import com.yi.model.objects.ModelUIConfig;

/**
 * @author long
 *
 */
public class ModelUIConfigApi implements IModelUIConfigApi{

	private MongoDBHelper dbHelper;
	public MongoDBHelper getDbHelper() {
		return dbHelper;
	}
	public void setDbHelper(MongoDBHelper dbHelper) {
		this.dbHelper = dbHelper;
	}
	@Override
	public String create(ModelUIConfig modelType) {
		// TODO Auto-generated method stub
		return dbHelper.addEntity(DBDefinitions.modelUIConfigsColl, convertModelTypeToEntity(modelType));
	}

	@Override
	public ModelUIConfig view(String id) {
		// TODO Auto-generated method stub
		return convertEntityToModelType(dbHelper.viewEntityById(DBDefinitions.modelUIConfigsColl, id));
	}
	
	@Override
	public ModelUIConfig viewByName(final String name){
		return convertEntityToModelType(dbHelper.viewEntity(DBDefinitions.modelUIConfigsColl, DBDefinitions.col_name, name));
	}
	
	@Override
	public void update(ModelUIConfig modelType) {
		dbHelper.updateEntity(DBDefinitions.modelUIConfigsColl, convertModelTypeToEntity(modelType));
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		dbHelper.removeEntityById(DBDefinitions.modelUIConfigsColl, id);
	}

	@Override
	public List<ModelUIConfig> list(final int modelTypeId) {
		// TODO Auto-generated method stub
		DBObject query = new BasicDBObject();
		query.put(DBDefinitions.col_type, modelTypeId);
		List<DBObject> entities = dbHelper.findSpecicialEntities(DBDefinitions.modelUIConfigsColl, 0,query);
		List<ModelUIConfig> modelTypes = new ArrayList<ModelUIConfig>();
		
		for(DBObject entity : entities){
			modelTypes.add(convertEntityToModelType(entity));
		}
		return modelTypes;
	}
	
	
	private DBObject convertModelTypeToEntity(ModelUIConfig modelType){
		DBObject entity = new BasicDBObject();
		if(StringUtils.hasLength(modelType.id))  entity.put(DBDefinitions.col_id, modelType.id);
		if(StringUtils.hasLength(modelType.name))  entity.put(DBDefinitions.col_name, modelType.name);
		if(StringUtils.hasLength(modelType.desc))  entity.put(DBDefinitions.col_desc, modelType.desc);
	    entity.put(DBDefinitions.col_type, modelType.type);
		try {
			if(StringUtils.hasLength(modelType.created))
				entity.put(DBDefinitions.col_created, new SimpleDateFormat("yyyy.MMMMM.dd GGG hh:mm:ss").parse(modelType.created));
			else
				entity.put(DBDefinitions.col_created, new SimpleDateFormat("yyyy.MMMMM.dd GGG hh:mm:ss").format(new Date()));
			
			if(StringUtils.hasLength(modelType.updated))
				entity.put(DBDefinitions.col_updated, new SimpleDateFormat("yyyy.MMMMM.dd GGG hh:mm:ss").parse(modelType.updated));
			else
				entity.put(DBDefinitions.col_updated, new SimpleDateFormat("yyyy.MMMMM.dd GGG hh:mm:ss").format(new Date()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(modelType.fields != null && modelType.fields.size() > 0){	
			
			entity.put(DBDefinitions.col_fields, modelType.fields);			
		}
		return entity;
	}
	
	private ModelUIConfig convertEntityToModelType(DBObject entity){
		ModelUIConfig modelType = new ModelUIConfig();
		if(entity.get(DBDefinitions.col_id) != null) modelType.id = entity.get(DBDefinitions.col_id).toString();
		if(entity.get(DBDefinitions.col_name) != null) modelType.name = entity.get(DBDefinitions.col_name).toString();
		if(entity.get(DBDefinitions.col_desc) != null) modelType.desc = entity.get(DBDefinitions.col_desc).toString();
		if(entity.get(DBDefinitions.col_created) != null) modelType.created = entity.get(DBDefinitions.col_created).toString();
		if(entity.get(DBDefinitions.col_updated) != null) modelType.updated = entity.get(DBDefinitions.col_updated).toString();
		if(entity.get(DBDefinitions.col_type) != null) modelType.type = (Integer)entity.get(DBDefinitions.col_type);
		if(entity.get(DBDefinitions.col_fields) != null) modelType.fields = (Map<String, Object>) entity.get(DBDefinitions.col_fields);
		return modelType;
	}

}
